package com.elm.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elm.dao.StoreDao;
import com.elm.dao.UserDao;
import com.elm.domain.Store;
import com.elm.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FreezeUtils {

    @Autowired
    private UserDao userDao;

    @Autowired
    private StoreDao storeDao;

    public static Map<String,Boolean> freezePeopleMap=new HashMap<>();

    public static Map<String,Boolean> freezeStoreMap=new HashMap<>();

    @PostConstruct
    public void initMap()
    {
        System.out.println("看看我自动执行了吗");
        //从数据库读取数据咯
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id")
                .gt("status",19);

        List<User> users = userDao.selectList(queryWrapper);

        for (int i = 0; i < users.size(); i++) {
            freezePeopleMap.put(users.get(i).getId(),true);
        }

        QueryWrapper<Store> storeQueryWrapper=new QueryWrapper<>();
        storeQueryWrapper.select("id")
                .eq("status",3);

        List<Store> stores = storeDao.selectList(storeQueryWrapper);

        for (int i = 0; i < stores.size(); i++) {
            freezeStoreMap.put(stores.get(i).getId(),true);
        }
    }

    public static boolean addToPeopleMap(String id)
    {
        freezePeopleMap.putIfAbsent(id, true);
        return true;
    }

    public static boolean addToStoreMap(String id)
    {
        freezeStoreMap.putIfAbsent(id,true);
        return true;
    }

    public static boolean deleteToPeopleMap(String id)
    {
        if(freezePeopleMap.get(id))  freezeStoreMap.remove(id);
        return true;
    }

    public static boolean deleteToStoreMap(String id)
    {
        if(freezeStoreMap.get(id)) freezeStoreMap.remove(id);
        return true;
    }

}
